package com.nc.cloud.authority.client.jwt;

import com.nc.cloud.authority.client.config.UserAuthConfig;
import com.nc.cloud.authority.common.util.jwt.IJWTInfo;
import com.nc.cloud.authority.common.util.jwt.JWTHelper;
import com.nc.cloud.common.exception.auth.UserTokenException;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * @Description  JWT存储对象
 * @ClassName UserAuthUtil
 * @Author NC—Cloud
 * @date 2020.03.29 20:30
 */
@Configuration
public class UserAuthUtil {

    @Autowired
    private UserAuthConfig userAuthConfig;

    public IJWTInfo getInfoFromToken(String token) throws Exception {
        try {
            System.out.print("getInfoFromToken:"+token+";userAuthConfig.getPubKeyByte():"+userAuthConfig.getPubKeyByte());
            return JWTHelper.getInfoFromToken(token, userAuthConfig.getPubKeyByte());
        }catch (ExpiredJwtException ex){
            throw new UserTokenException("User token expired!");
        }catch (SignatureException ex){
            throw new UserTokenException("User token signature error!");
        }catch (IllegalArgumentException ex){
            throw new UserTokenException("User token is null or empty!");
        }
    }

}
